package com.stylefeng.guns.modular.system.controller.server;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.CategoryMapper;
import com.stylefeng.guns.common.persistence.model.Category;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.CategoryDao;
import com.stylefeng.guns.modular.system.warpper.CategoryWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lmshe
 * @create 2017-11-08 11:03
 * @desc
 */
@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController {

    private static String PREFIX = "/system/category/";

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 跳转到文档分类管理页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "category.html";
    }


    /**
     * 跳转到添加文档分类
     */
    @RequestMapping("/category_add")
    public String categoryAdd() {
        return PREFIX + "category_add.html";
    }

    /**
     * 跳转到修改文档分类
     */
    @RequestMapping("/category_update/{categoryId}")
    public String categoryUpdate(@PathVariable Integer categoryId, Model model) {

        Category category = this.categoryMapper.selectById(categoryId);
        Category pCategory = this.categoryMapper.selectById(category.getParentId());
        model.addAttribute("category", category);
        String pName = "";
        if(pCategory != null){
            pName = pCategory.getCategoryName();
        }
        model.addAttribute("pName", pName);
        LogObjectHolder.me().set(category);
        return PREFIX + "category_edit.html";
    }

    /**
     * @author lmshe
     * @create 11:20 2017/11/8
     * @desc 查询文档分类列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String categoryName) {
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        List<Map<String, Object>> result = categoryDao.getCategoryList(page, categoryName, page.getOrderByField(), page.isAsc());
        page.setRecords((List<Map<String, Object>>) new CategoryWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增文档分类
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增文档分类", key = "categoryName")
    public Object add(Category category) {
        if (ToolUtil.isOneEmpty(category, category.getCategoryName())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        if (ToolUtil.isEmpty(category.getParentId())) {
            category.setParentId(0);
        }
        category.setCreatetime(new Date());
        category.insert();
        return super.SUCCESS_TIP;
    }

    /**
     * 删除文档分类
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除分类", key = "categoryId")
    public Object delete(@RequestParam Integer categoryId) {
        Category category = this.categoryMapper.selectById(categoryId);
        category.setIsDelete(1);
        category.updateById();
        return super.SUCCESS_TIP;
    }
    /**
     * 删除文档分类
     */
    @RequestMapping(value = "/cancel_delete")
    @ResponseBody
    @BussinessLog(value = "取消删除分类", key = "categoryId")
    public Object cancelDelete(@RequestParam Integer categoryId) {
        Category category = this.categoryMapper.selectById(categoryId);
        category.setIsDelete(0);
        category.updateById();
        return super.SUCCESS_TIP;
    }
    /**
     * 修改文档分类
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改文档分类", key = "categoryName")
    public Object update(Category category) {
        if (ToolUtil.isOneEmpty(category, category.getId(), category.getCategoryName())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        if (ToolUtil.isEmpty(category.getParentId())) {
            category.setParentId(0);
        }
        this.categoryMapper.updateById(category);
        return super.SUCCESS_TIP;
    }

    /**
     * 获取分类的tree列表(父级)
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.categoryDao.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }
    /**
     * 获取分类的tree列表（所有级别）
     */
    @RequestMapping(value = "/tree_all")
    @ResponseBody
    public List<ZTreeNode> treeAll() {
        List<ZTreeNode> tree = this.categoryDao.treeAll();
        tree.add(ZTreeNode.createAll());
        return tree;
    }

    /**
     * 获取分类列表
     */
    @RequestMapping(value = "/category_list")
    @ResponseBody
    @BussinessLog(value = "获取子分类列表", key = "categoryId")
    public List<Category> treeAll(@RequestParam Integer categoryId) {
        return this.categoryMapper.selectList(new EntityWrapper<Category>().eq("parent_id", categoryId));
    }
}
